package com.xiaotu.spider.maoyan.pageprocessor;
/***
 * 猫眼电影短评最热500条
 * @author Administrator
 *
 */

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.xiaotu.common.model.MaoYanMovieScoreModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.KeywordEncode;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.UUIDUtils;
import com.xiaotu.server.model.maoyan.MovieTagModel;
import com.xiaotu.server.service.maoyan.MaoYanMovieScoreService;
import com.xiaotu.spider.AUrlPageProcessor;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

@Component("MaoYanMovieScorePageProcessor")
@Scope("prototype")
public class MaoYanMovieScorePageProcessor extends AUrlPageProcessor{

	@Autowired
	private MaoYanMovieScoreService maoYanMovieScoreService;
	
	private List<String> scoreList = Arrays.asList(new String[]{"1-2分","3-4分","5-6分","7-8分","9-10分"});
	
	private List<String> sexList = Arrays.asList(new String[]{"男","女"});
	
	private List<String> ageList = Arrays.asList(new String[]{"20岁以下","20-24岁","25-29岁","30-34岁","35-39岁","40岁以上"});
	
	private List<String> cityList = Arrays.asList(new String[]{"一线城市","二线城市","三线城市","四线城市"});
	
	@Override
	public void process(Page page) {
		
		Map<String, Object> movieMap = page.getRequest().getExtras();
		
		switch (this.getPageDataType(page))
        {
            case Constants.PageDataType.TV_COMMENT:
                this.downLoadData(page,movieMap);
                break;
            case "getMovieId":
                this.addRequest(page,movieMap);
                break;
            case "searchPage":
            	this.searchPage(page,movieMap);
            	break;
            default:
                break;
        }
	}

	
	//用于获取数据
	@SuppressWarnings("static-access")
	private void downLoadData(Page page,Map<String, Object> movieMap) {
		//电影名
		String movieName = movieMap.get("movieName").toString();
		
		List<MovieTagModel> tagList = new ArrayList<MovieTagModel>();
		List<MovieTagModel> tagRdisList = new ArrayList<MovieTagModel>();
		
		//获取需要爬去的数据
		List<String> pShortCommentTagsList = new Html(page.getRawText()).xpath("//div[@class='movie-tags']/span").all();
		for(String tags : pShortCommentTagsList){
			String tagsTitle = new Html(tags).xpath("//span/text()").get();
			if(StringUtils.isNotBlank(tagsTitle)){
				tagList.add(new MovieTagModel(UUIDUtils.getId(),tagsTitle,null));
			}
		}
		String [] tagRedArr = new String[tagList.size()];
		List<MovieTagModel> tagMapList = maoYanMovieScoreService.queryAllTagsByName(tagList);
		for(int i=0; i<tagList.size();i++){
			String currName = tagList.get(i).getTagname();
			for(MovieTagModel movieTagModel : tagMapList){
				if(movieTagModel.getTagname().equals(currName)){
					tagRdisList.add(movieTagModel);
					tagList.remove(i);
					i--;
					break;
				}
			}
		}
		tagRdisList.addAll(tagList);
		//批量增加标签
		if(tagList.size()>0){
			maoYanMovieScoreService.saveBatch(tagList);
		}else{
			tagRdisList = tagMapList;
		}
		for(int i=0;i<tagRdisList.size();i++){
			tagRedArr[i] = tagRdisList.get(i).getId();
		}
		//打分范围
		double [] scoreRangeArr = new double[5];
		List<String> pShortCommentScoreList = page.getHtml().xpath("//div[@class='distribute-chart']/div[@class='distribute-item']").all();
		for(String score : pShortCommentScoreList){
			Html scoreHtml = new Html(score);
			String scoreFlag = scoreHtml.xpath("//div[@class='distribute-label']/text()").get();
			String scoreValue = scoreHtml.xpath("//div[@class='distribute-bar-bg']/span[@class='distribute-val']/text()").get();
			if(scoreList.indexOf(scoreFlag.trim())!=-1){
				scoreRangeArr[scoreList.indexOf(scoreFlag.trim())] = Double.parseDouble(scoreValue.replace("%", ""));
			}
		}
		
		/***
		 * 观众画像
		 */
		//男，女
		double [] sexRangeArr = new double[2];
		//年龄范围
		double [] ageRangeArr = new double[6];
		//城市级别
		double [] cityRangeArr = new double[4];
		/***
		 * 截取观众画像之后的字符串
		 */
		String pageStr = page.getRawText();
		if(StringUtils.isNotBlank(pageStr)){
			pageStr = pageStr.substring(pageStr.indexOf("<h2>观众画像</h2>")+13,pageStr.lastIndexOf("</section>"));
			String [] pageArr = pageStr.split("<i class=\"cs\">");
			for(int i=0;i< pageArr.length;i++){
				if(pageArr[i].contains("男")){
					String temp = pageArr[i+1].substring(0, pageArr[i+1].indexOf("分"));
					//sexRangeArr[0] = ;
				}
				else if(pageArr[i].contains("女")){
					String temp = pageArr[i+1].substring(0, pageArr[i+1].indexOf("分"));
					//sexRangeArr[1] = ;
				}
				else if(pageArr[i].contains("20岁以下")){
					String temp = pageArr[i+1].substring(0, pageArr[i+1].indexOf("分"));
					//ageRangeArr[0] = ;
				}
				else if(pageArr[i].contains("20-24岁")){
					String temp = pageArr[i+1].substring(0, pageArr[i+1].indexOf("分"));
					//ageRangeArr[1] = ;
				}
				else if(pageArr[i].contains("25-29岁")){
					String temp = pageArr[i+1].substring(0, pageArr[i+1].indexOf("分"));
					//ageRangeArr[2] = ;
				}
				else if(pageArr[i].contains("30-34岁")){
					String temp = pageArr[i+1].substring(0, pageArr[i+1].indexOf("分"));
					//ageRangeArr[3] = ;
				}
				else if(pageArr[i].contains("35-39岁")){
					String temp = pageArr[i+1].substring(0, pageArr[i+1].indexOf("分"));
					//ageRangeArr[4] = ;
				}
				else if(pageArr[i].contains("40岁以上")){
					String temp = pageArr[i+1].substring(0, pageArr[i+1].indexOf("分"));
					//ageRangeArr[5] = ;
				}
				else if(pageArr[i].contains("一线城市")){
					String temp = pageArr[i+1].substring(0, pageArr[i+1].indexOf("分"));
					//cityRangeArr[0] = ;
				}
				else if(pageArr[i].contains("二线城市")){
					String temp = pageArr[i+1].substring(0, pageArr[i+1].indexOf("分"));
					//cityRangeArr[1] = ;
				}
				else if(pageArr[i].contains("三线城市")){
					String temp = pageArr[i+1].substring(0, pageArr[i+1].indexOf("分"));
					//cityRangeArr[2] = ;
				}
				else if(pageArr[i].contains("四线城市")){
					String temp = pageArr[i+1].substring(0, pageArr[i+1].indexOf("分"));
					//cityRangeArr[3] = ;
				}
			}
			
		}
		
		//存放数据
		MaoYanMovieScoreModel maoYanMovieScoreModel = new MaoYanMovieScoreModel(
				UUIDUtils.getId(),
				movieName,
				tagRedArr,
				scoreRangeArr,
				sexRangeArr,
				ageRangeArr,
				cityRangeArr
				);
		page.putField(MaoYanMovieScoreModel.class.getName(), maoYanMovieScoreModel);
		
	}
	
	//搜索页面
	private void searchPage(Page page,Map<String, Object> movieMap){
		//搜索是否有结果
		if(!page.getHtml().xpath("//div[@class='search-result-box']").get().contains("empty-list")){
			String movieName = page.getHtml().xpath("//input[@class='kw']/@value").get();
			if(StringUtils.isNotBlank(movieName)){
				movieMap.put("movieName", movieName);
				//获取总页数
				int maxPageNum = 0;
				if(StringUtils.isNotBlank(page.getHtml().xpath("//ul[@class='list-pager']/").get())){
					List<Integer> pageNumList = new ArrayList<Integer>();
					
					for(String liStr :page.getHtml().xpath("//ul[@class='list-pager']/li").all()){
						Html liHtml = new Html(liStr);
						int pageNum = 0;
						try {
							pageNum = Integer.parseInt(liHtml.xpath("//a/text()").get());
						} catch (Exception e) {
							continue;
						}
						pageNumList.add(pageNum);
					}
					Collections.sort(pageNumList);
					maxPageNum = pageNumList.get(pageNumList.size()-1);
				}
				
				List<String> requestUrlList = new ArrayList<String>();
				
				if(maxPageNum > 0){
					for(int i=1;i<=maxPageNum;i++){
						requestUrlList.add("https://maoyan.com/query?kw="+KeywordEncode.encode(movieName)+"&offset="+((i-1)*20));
					}
				}else{
					requestUrlList.add("https://maoyan.com/query?kw="+KeywordEncode.encode(movieName)+"&offset="+0);
				}
				PageUtils.addTargetRequests(page,requestUrlList,movieMap);
			}
		}
	}
	
	
	
	//用于添加新的请求
	private void addRequest(Page page,Map<String, Object> movieMap) {
		//获取当前电影的movieId
		//当前电影名字
		String movieName = movieMap.get("movieName")==null?"":movieMap.get("movieName").toString();
		List<String> ddStrList = page.getHtml().xpath("//dl[@class='movie-list']/dd").all();
		for(String ddStr : ddStrList){
			Html ddHtml = new Html(ddStr);
			String eachMovieName = ddHtml.xpath("//div[@class='channel-detail movie-item-title']/@title").get();
			if(movieName.equals(eachMovieName)){
				movieMap.put("movieName", eachMovieName);
				//获取movieId
				String movieId = ddHtml.xpath("//div[@class='channel-detail movie-item-title']/a/@href").get();
				movieId = movieId.substring(movieId.lastIndexOf("/")+1);
				PageUtils.addTargetRequest(page,"https://piaofang.maoyan.com/movie/"+movieId+"/audienceRating?usePageCache=true",movieMap);
				break;
			}
		}
		
	}
		
	
	@SuppressWarnings("deprecation")
	@Override
	protected String[] getStartRequestUrls() {
		String[] urlArr = {
				"https://maoyan.com/query?kw="+KeywordEncode.encode("巨齿鲨"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("我不是药神"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("前任3：再见前任"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("影"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("战狼2"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("网络谜踪"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("西虹市首富"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("流浪地球"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("头号玩家"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("三块广告牌"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("疯狂的外星人"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("捉妖记2"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("幕后玩家"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("一出好戏"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("超时空同居"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("来电狂响"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("邪不压正"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("无间道"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("神奇动物在哪里"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("纯洁心灵·逐梦演艺圈"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("让子弹飞"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("无名之辈"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("动物世界"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("叶问外传：张天志")
		};
		return urlArr;
	}
	
	@Override
	protected String getSpiderType() {
		return Constants.SpiderTaskType.MOVIE_MAOYAN_SCORE;
	}

	@Override
	public String getPageDataType(Page page) {
		String url = page.getUrl().toString();
		String dataType = "";
		if (url.contains("https://piaofang.maoyan.com/movie/")){
			dataType = Constants.PageDataType.TV_COMMENT;
	    }
        else if (url.contains("https://maoyan.com/query?kw=")){
        	dataType = "searchPage";
        	if (url.contains("offset")){
            	dataType = "getMovieId";
            }
        };
        return dataType;
	}
	
}
